package com.hq.talk.presenter.impl;

import android.content.Context;

import com.hq.lib_comview.common.code.CodeStatus;
import com.hq.lib_comview.common.code.ReturnCode;
import com.hq.lib_comview.common.utils.NetUtil;
import com.hq.talk.R;
import com.hq.talk.common.ConstantTag;
import com.hq.talk.common.Session;
import com.hq.talk.http.BaseCallback;
import com.hq.talk.http.CallManager;
import com.hq.talk.http.HandleRetCode;
import com.hq.talk.http.Http;
import com.hq.talk.http.ResponseData;
import com.hq.talk.http.request.APIService;
import com.hq.talk.model.CompanyModel;
import com.hq.talk.model.UserInfoModel;
import com.hq.talk.presenter.GeneralPresenter;
import com.hq.talk.presenter.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 描述 ：处理用户信息
 * 作者 ：hq
 * 时间 ：2017/8/8 22:01
 */
public class CompanyPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mService;

    @Override
    public Object getCacheData() {
        return null;
    }

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mService = Http.getUserService(mContext);
    }

    /**
     * 搜索公司
     */
    public void searchCompany(final String companyName) {
        if (!NetUtil.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "");
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "");
        Call<ResponseData<List<CompanyModel>>> call = mService.searchCompany(Session.getUserId(), Session.getUserToken(), companyName);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CompanyModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CompanyModel>>> call, Response<ResponseData<List<CompanyModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CompanyModel>> res = response.body();
                    if (res.getCode() == ReturnCode.SUCCESS) {
                        List<CompanyModel> companys = res.getData();
                        if (null != companys) {
                            if (companyName.isEmpty()) {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "");
                            } else {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                                mView.updateViewWithTag(companys, ConstantTag.Company.GET_COMPANYS);
                            }
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
            }

            @Override
            public void onFailure(Call<ResponseData<List<CompanyModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
            }
        });
    }

    @Override
    public void detachView() {

    }
}
